package com.kylin.core.commodity.event.listener;

import com.kylin.core.commodity.event.repository.CommodityDataHandler;
import com.kylin.domain.commodity.event.CommodityCommentedEvent;
import com.kylin.domain.commodity.event.CommodityCreatedEvent;
import com.kylin.domain.commodity.event.CommodityUpdatedEvent;
import com.kylin.domain.commodity.vo.CommentVO;
import com.kylin.shared.enums.YesNoEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.eventhandling.EventHandler;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 商品SPU事件监听器
 * @author Damon S.
 * @version v1.0.1
 * @date 2019年11月06日 14:53
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CommodityEventListener {

    private final CommodityDataHandler commodityDataHandler;


    @EventHandler
    @Transactional(rollbackFor = Exception.class)
    void on(CommodityCreatedEvent event) {
        // 保存商品信息
        this.commodityDataHandler.create(event);
        // 保存商品图集
        if (CollectionUtils.isEmpty(event.getAlbumVO())) {
            this.commodityDataHandler.create(event.getAlbumVO());
        }
    }

    @EventHandler
    @Transactional(rollbackFor = Exception.class)
    void on(CommodityUpdatedEvent event) {
    }

    @EventHandler
    @Transactional(rollbackFor = Exception.class)
    void on(CommodityCommentedEvent event) {
        this.commodityDataHandler.create(
                CommentVO.builder()
                .userId(event.getCommentedBy())
                .commodityId(event.getCommodityId())
                .content(event.getContent())
                        // TODO: nickname & avatar
                .createdAt(event.getCommentedAt())
                .removed(YesNoEnum.N).build());
    }

//    @EventHandler
//    @Transactional(rollbackFor = Exception.class)
//    void on(SpuShelvedEvent event) {
//    }
//
//    @EventHandler
//    @Transactional(rollbackFor = Exception.class)
//    void on(SpuPutOffedEvent event) {
//
//    }
//
//    @EventHandler
//    @Transactional(rollbackFor = Exception.class)
//    void on(SpuRemovedEvent event) {
//
//    }
//
//    @EventHandler
//    @Transactional(rollbackFor = Exception.class)
//    void on(SpuApprovedEvent event) {
//
//    }
//
//    @EventHandler
//    @Transactional(rollbackFor = Exception.class)
//    void on(SpuRejectedEvent event) {
//
//    }
}
